//
//  MJHomeViewController.m
//  MJ
//
//  Created by qiaoBu on 2018/1/18.
//  Copyright © 2018年 lina. All rights reserved.
//

#import "MJHomeViewController.h"
#import "MJNetWorkTool.h"
#import "MJHomeModel.h"
#import "MJHomeViewModel.h"
#import <YYKit.h>
#import <SDCycleScrollView.h>
#import "MJHomeCarouseModel.h"
#import "iCarousel.h"
#import "MJQuizzesView.h"
#import "MJMeViewController.h"
#import "MJExamViewController.h"
#import "MJLoginViewController.h"

@interface MJHomeViewController ()<iCarouselDelegate,iCarouselDataSource>

/** pageControl */
@property (nonatomic, strong) UIPageControl *pageControl;

/**
 测试题轮播
 */
@property (nonatomic, strong) iCarousel *carouselBanner;

/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** model */
@property (nonatomic, strong) NSMutableArray <Quizzes *> *models;

/** page */
@property (nonatomic, assign) NSInteger page;

@end

@implementation MJHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    _page = 1;
    [self fetchData];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)setupUI{
    self.navgationBar.titleLabel.text = @"爱情测试";
    self.view.backgroundColor = HEXColor(@"#ff8280");
    [self.view insertSubview:self.carouselBanner belowSubview:self.navgationBar];
    self.navgationBar.leftNavigatoinItem.left += 10;
    [self.carouselBanner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(10);
        make.width.mas_equalTo(kScreenWidth-40);
        if (kScreenHeight == 812) {
            make.height.mas_equalTo((kScreenWidth-50)/kScreenWidth*(kScreenHeight)+20);
        }else{
            make.height.mas_equalTo((kScreenWidth-50)/kScreenWidth*(kScreenHeight));
        }
        NSLog(@"--%f---%f",kScreenHeight,kScreenHeight);
    }];
    [self.navgationBar.leftNavigatoinItem setImage:[UIImage imageNamed:@"my"] forState:UIControlStateNormal];
//    [self.navgationBar.leftNavigatoinItem addTarget:self action:@selector(meClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navgationBar.rightNavigatoinItem setImage:[UIImage imageNamed:@"refresh"] forState:UIControlStateNormal];
    [self.navgationBar.rightNavigatoinItem addTarget:self action:@selector(refreshClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.navgationBar.leftNavigatoinItem mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(10);
        //        make.size.mas_equalTo(CGSizeMake(48, 48));
        //        make.top.mas_equalTo(self).offset(self.navgationBar.height-44);
    }];
    
    [self.navgationBar.rightNavigatoinItem mas_updateConstraints:^(MASConstraintMaker *make) {
        
        //        make.height.mas_equalTo(self.navgationBar.leftNavigatoinItem);
        make.right.mas_equalTo(self.view.mas_right).offset(-2);
        //        make.width.mas_equalTo(70);
        //        make.centerY.mas_equalTo(self.navgationBar.leftNavigatoinItem);;
        
    }];
    // pageControl
    _pageControl = [[UIPageControl alloc] init];
    [self.view addSubview:_pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.carouselBanner).offset(15);
        make.right.mas_equalTo(self.carouselBanner).offset(-15);
        make.centerX.mas_equalTo(self.view);
        //        make.top.mas_equalTo(self.carouselBanner.mas_bottom).offset(20);
        if (kScreenHeight == 812) {
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-65);
        }else{
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-15);
        }
    }];
    [_pageControl sizeToFit];
}

#pragma mark -iCarouselDelegate

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel{
    [self.pageControl setCurrentPage:carousel.currentItemIndex];
}

-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    return self.models.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    if (view == nil) {
        
        MJQuizzesView *quizzesView = [[MJQuizzesView alloc]init];
        quizzesView.backgroundColor = [UIColor whiteColor];
        if (kScreenHeight == 812) {
            quizzesView.frame = CGRectMake(0, 0, self.view.width - 50, (kScreenWidth-40)/650*(398+582)+20);
        }else{
            quizzesView.frame = CGRectMake(0, 0, self.view.width - 50, (kScreenWidth-40)/650*(398+582));
        }
        quizzesView.quizzes = self.models[index];
        [quizzesView.startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        return quizzesView;
    }
    
    return nil;
}

- (void)start{
    [self carousel:self.carouselBanner didSelectItemAtIndex:self.carouselBanner.currentItemIndex];
}

- (CGFloat)carousel:(iCarousel *)carousel
     valueForOption:(iCarouselOption)option
        withDefault:(CGFloat)value
{
    if (option == iCarouselOptionSpacing)
    {
        return value * 1.f;
    }
    return value;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index;
{
    //    if (![[NSUserDefaults standardUserDefaults] boolForKey:IsLogined]) {
    //        MJLoginViewController *loginVc = [[MJLoginViewController alloc] init];
    //        [self.navigationController pushViewController:loginVc animated:YES];
    //    } else {
    //        MJExamViewController *quizzesVc = [[MJExamViewController alloc] init];
    //        quizzesVc.quizzes = self.models[index];
    //        quizzesVc.quizzesArr = self.models;
    //        [self.navigationController pushViewController:quizzesVc animated:YES];
    //    }
    
    MJExamViewController *quizzesVc = [[MJExamViewController alloc] init];
    quizzesVc.quizzes = self.models[index];
    quizzesVc.quizzesArr = self.models;
    [self.navigationController pushViewController:quizzesVc animated:YES];
}

-(CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform{
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.9f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    return CATransform3DTranslate(transform, offset * self.carouselBanner.itemWidth * 1.1, 0.0, 0.0);
}

- (void)fetchData{
    
    [MJHomeViewModel requestHomeListDataWithPage:_page Success:^(MJHomeModel *homeModel) {
        [self.models removeAllObjects];
        self.models = [NSMutableArray arrayWithArray:homeModel.list];
        [self.carouselBanner reloadData];
        [self.pageControl setNumberOfPages:self.models.count];
        if ([homeModel.page_total integerValue] > _page) {
            _page++;
        }else{
            _page = 1;
        }
    } failure:^(NSError *error) {
        
    } netWorkState:^(BOOL netWorkState) {
        
    }];
}


#pragma mark - lazy
- (iCarousel *)carouselBanner
{
    if (!_carouselBanner) {
        _carouselBanner = [[iCarousel alloc]init];
        _carouselBanner.type = iCarouselTypeCustom;
        _carouselBanner.delegate = self;
        _carouselBanner.dataSource = self;
        _carouselBanner.bounces = NO;
        _carouselBanner.pagingEnabled = YES;
    }
    return _carouselBanner;
}

- (void)meClick{
    
    //   if ([[NSUserDefaults standardUserDefaults] boolForKey:IsLogined])
    //   {
    //
    //    MJMeViewController *meVc = [[MJMeViewController alloc] init];
    //    [self.navigationController pushViewController:meVc animated:YES];
    //   }
    //   else
    //   {
    //       MJLoginViewController *loginController = [[MJLoginViewController alloc] init];
    //       [self.navigationController pushViewController:loginController animated:YES];
    //   }
    
    MJMeViewController *meVc = [[MJMeViewController alloc] init];
    [self.navigationController pushViewController:meVc animated:YES];
    
}
- (void)back{
    
    MJMeViewController *meVc = [[MJMeViewController alloc] init];
    [self.navigationController pushViewController:meVc animated:YES];
    
}

- (void)refreshClick{
    
    [self fetchData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
